package com.example.gzysmartcity.viewmodel;

import androidx.lifecycle.ViewModel;

import com.example.gzysmartcity.repository.local.SellCarDB;
import com.example.gzysmartcity.repository.model.SellCar;

import java.util.List;

public class SellCarViewModel extends ViewModel {
    private final List<SellCar> mSellCarList = SellCarDB.loadAll();


    public double getSumNumAll() {
        double num = 0;
        for (SellCar s : mSellCarList) {
            num += s.getNum() * s.getMoney();
        }
        return num;
    }


    public double getPrice(int p) {
        SellCar sellCar = mSellCarList.get(p);
        return sellCar.getNum() * sellCar.getMoney();
    }

    public double DeleteSellCar(int p) {
        SellCar sellCar = mSellCarList.get(p);
        SellCarDB.del(sellCar);
        mSellCarList.remove(p);
        return sellCar.getMoney() * sellCar.getNum();
    }

    public void DeleteSomeSellCar(List<Boolean> list) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i)) {
                SellCar sellCar = mSellCarList.get(i);
                SellCarDB.del(sellCar);
                list.set(i, false);
            }
        }
        mSellCarList.clear();
        mSellCarList.addAll(SellCarDB.loadAll());
    }

    public double AddSellCarNum(int p) {
        SellCar sellCar = mSellCarList.get(p);
        sellCar.setNum(sellCar.getNum() + 1);
        SellCarDB.update(sellCar);
        mSellCarList.set(p, sellCar);
        return sellCar.getMoney();
    }

    public double ReduceSellCarNum(int p) {
        SellCar sellCar = mSellCarList.get(p);
        if (sellCar.getNum() == 1) {
            return 0;
        }
        sellCar.setNum(sellCar.getNum() - 1);
        SellCarDB.update(sellCar);
        mSellCarList.set(p, sellCar);
        return sellCar.getMoney();
    }

    public List<SellCar> getSellCarList() {
        return mSellCarList;
    }

    public int getSellCarListSize() {
        return mSellCarList.size();
    }
}
